package org.fruitrecognitionbasedonai.fruitrecognitionbasedonai.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.fruitrecognitionbasedonai.fruitrecognitionbasedonai.entity.Record;
import org.fruitrecognitionbasedonai.fruitrecognitionbasedonai.entity.RespBean;
import org.fruitrecognitionbasedonai.fruitrecognitionbasedonai.entity.UserRecord;
import org.fruitrecognitionbasedonai.fruitrecognitionbasedonai.service.IRecordService;
import org.fruitrecognitionbasedonai.fruitrecognitionbasedonai.service.IUserRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Re
 * @Date: 2021/6/20 0:17
 */
@RestController
@RequestMapping("/record")
@Api(tags = "记录接口",value = "RecordController")
@Slf4j
public class RecordController {
    @Autowired
    protected IUserRecordService iUserRecordService;
    @Autowired
    protected IRecordService iRecordService;
    @PostMapping("/insert")
    @ApiOperation(value = "增加记录")
    public RespBean insert(@Valid Record record,Integer userId, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            Map<String, Object> map = new HashMap<>();
            List<FieldError> errors = bindingResult.getFieldErrors();
            log.error("插入失败！");
            for (FieldError error : errors) {
                log.error("错误的字段名：" + error.getField());
                log.error("错误信息：" + error.getDefaultMessage());
                map.put(error.getField(), error.getDefaultMessage());
            }
            return RespBean.error(map);
        }
        if (iRecordService.insert(record)) {
            Integer recordId = iRecordService.selectId(record);
            UserRecord userRecord = new UserRecord();
            userRecord.setRecordId(recordId);
            userRecord.setUserId(userId);
            if (iUserRecordService.insert(userRecord)) {
                log.info("添加成功");
                return RespBean.ok("添加成功");
            } else {
                log.info("添加关联信息失败");
                return RespBean.error("添加关联信息失败");
            }
        }
        log.info("添加失败");
        return RespBean.error("添加失败");
    }
    @DeleteMapping("/deleteByRecordId")
    @ApiOperation(value = "根据记录id删除记录")
    public RespBean deleteByRecordId(Integer recordId) {
        if (iRecordService.deleteById(recordId)) {
            if (iUserRecordService.deleteByRecordId(recordId)) {
                log.info("删除成功");
                return RespBean.ok("删除成功");
            } else {
                log.info("删除关联信息失败");
                return RespBean.error("删除关联信息失败");
            }
        } else {
            log.info("删除失败");
            return RespBean.error("删除失败");
        }
    }
    @DeleteMapping("/deleteByUserId")
    @ApiOperation(value = "根据用户id删除记录")
    public RespBean deleteByUserId(Integer userId) {
        if (iRecordService.deleteList(userId)) {
            if (iUserRecordService.deleteByUserId(userId)) {
                log.info("删除成功");
                return RespBean.ok("删除成功");
            } else {
                log.info("删除关联信息失败");
                return RespBean.error("删除关联信息失败");
            }
        } else {
            log.info("删除失败");
            return RespBean.error("删除失败");
        }
    }
    @GetMapping("/selectAll")
    @ApiOperation(value = "查询所有记录")
    public RespBean selectAll() {
        List<Record> recordList = iRecordService.selectAll();
        Map<String ,Object> map = new HashMap<>();
        if (recordList.size() != 0) {
            map.put("recordList",recordList);
            log.info("查询成功");
            return RespBean.ok("查询成功",map);
        } else {
            log.info("暂无信息");
            return RespBean.ok("暂无信息");
        }
    }
    @GetMapping("/selectByUserId")
    @ApiOperation(value = "查询用户所有记录")
    public RespBean selectByUserId(Integer userId) {
        List<Record> recordList = iRecordService.selectByUserId(userId);
        Map<String ,Object> map = new HashMap<>();
        if (recordList.size() != 0) {
            map.put("recordList",recordList);
            log.info("查询成功");
            return RespBean.ok("查询成功",map);
        } else {
            log.info("暂无信息");
            return RespBean.ok("暂无信息");
        }
    }
}
